#python版本:3.12

#python.exe -m pip install --upgrade pip
#需要額外引入的庫：
#pip install opencv-python
#pip install paddlepaddle
#pip install scikit-image
#pip install paddleocr
#pip install setuptools
#pip install requests
#pip install premailer
#pip install openpyxl

import os
import cv2
from paddleocr import PPStructure, save_structure_res
from skimage import io

table_engine = PPStructure(show_log=True)  #开启PPStructure的日志功能
save_folder = './output'  #识别出的excel文件存放的位置


def findAllPng(floderPath):  #找出floderPath中所有的png文件，并将png的文件名保存到paths中返回
    paths = []
    img_path = sorted(
        [os.path.join(floderPath, name) for name in os.listdir(floderPath) if
         name.endswith('.png')])  ###这里的'.png'可以换成任意的文件后缀
    for i in range(len(img_path)):
        img_name = os.path.split(img_path[i])[-1]
        img_name = img_name.split('.')[0]
        img = io.imread(img_path[i])
        paths.append(img_name)
    return paths


def pngToExcel(paths):  #將paths中所有png進行表格識別並保存到save_folder中
    for path in paths:
        img_path = path + '.png'
        img = cv2.imread(img_path)
        result = table_engine(img)
        save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0])


#folderPath = r'D:\test1\pythonProject'
folderPath = r'D:\Program_Files\Project\Python\python-demo\python\PngToExcel' #png文件存放的位置
paths = findAllPng(folderPath)
# print(paths)
pngToExcel(paths)
